package cn.like.backEnd.config;

import cn.like.base.auth.AuthContext;
import cn.like.base.support.id.IdGenerator;
import cn.like.datasource.BaseMybatisConfiguration;
import cn.like.datasource.support.CustomIdGenerator;
import cn.like.datasource.support.MyMetaObjectHandler;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * desc:
 * Mybatis相关配置
 * <br>
 * details:
 *
 * @author like 980650920@qq.com
 * @date 2021-07-08 10:18:00
 * @see BaseMybatisConfiguration
 */
@Configuration
@Slf4j
public class AdminMybatisAutoConfiguration extends BaseMybatisConfiguration {

    final IdGenerator idGenerator;
    final AuthContext  authContext;

    public AdminMybatisAutoConfiguration(IdGenerator idGenerator, AuthContext authContext) {
        this.idGenerator = idGenerator;
        this.authContext = authContext;
    }

    /**
     * 分页插件，自动识别数据库类型
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 添加自定义id生成器
     */
    @Bean
    public IdentifierGenerator plusPropertiesCustomizer() {
        return new CustomIdGenerator(idGenerator);
    }

    /**
     * 自动填充功能
     */
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MyMetaObjectHandler(idGenerator, authContext);
    }

}